<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="phone">
			<el-input v-model="form.phone" prefix-icon="el-icon-iphone" clearable :placeholder="$t('login.mobilePlaceholder')">
				<template #prepend>+86</template>
			</el-input>
		</el-form-item>
		<el-form-item prop="yzm"  style="margin-bottom: 35px;">
			<div class="login-msg-yzm">
				<el-input v-model="form.yzm" prefix-icon="el-icon-unlock" clearable :placeholder="$t('login.smsPlaceholder')"></el-input>
				<el-button @click="getYzm" :disabled="disabled">{{this.$t('login.smsGet')}}<span v-if="disabled"> ({{time}})</span></el-button>
			</div>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
		<div class="login-reg">
			{{$t('login.noAccount')}} <router-link to="/user_register">{{$t('login.createAccount')}}</router-link>
		</div>
	</el-form>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					phone: "",
					yzm: "",
				},
				rules: {
					phone: [
						{required: true, message: this.$t('login.mobileError')}
					],
					yzm: [
						{required: true, message: this.$t('login.smsError')}
					]
				},
				disabled: false,
				time: 0,
				islogin: false,
			}
		},
		mounted() {

		},
		methods: {
			async getYzm(){
				//参数验证

				var validate = await this.$refs.loginForm.validateField("phone").catch(()=>{})
				if(!validate){ return false }
				//发送的数据
				
				var data = {
					phone: this.form.phone,
				
				}
				//发起异步请求,获取验证码
				//this.$API 固定的
				//auth      api/auth.js模块
				//token     api/auth.js模块中tokenSms对象
				var res = await this.$API.auth.getSmsCode.post(data)

				if(res.code == 200){
					//发送成功
					this.$message.success(this.$t('login.smsSent'))
					//禁用按钮
					this.disabled = true
					//开始倒计时60秒
					this.time = 60
					var t = setInterval(() => {
						this.time -= 1
						if(this.time < 1){
							//清除倒计时
							clearInterval(t)
							//恢复按钮单击功能
							this.disabled = false
							//重置倒计时
							this.time = 0
						}
					},1000)
				}
				
				
				
			},
			async login(){
				var validate = await this.$refs.loginForm.validate().catch(()=>{})
				if(!validate){ return false }
				//发起异步请求
				this.islogin = true;
				//提交的数据：手机号码+验证码
				let data = {
					phone:this.form.phone,
					smsCode:this.form.yzm
				}
				var user = await this.$API.auth.token.post(data)
				if(user.code == 200){
					this.$TOOL.cookie.set("TOKEN", user.data.token, {
						expires: this.form.autologin? 24*60*60 : 0
					})
					this.$TOOL.data.set("USER_INFO", user.data.userInfo)
				}else{
					this.islogin = false
					// 消息组件
					this.$message.warning(user.message)
					return false
				}
				//获取菜单
				var menu = null
				if(this.form.user == 'admin'){
					menu = await this.$API.system.menu.myMenus.get()
				}else{
					menu = await this.$API.demo.menu.get()
				}
				if(menu.code == 200){
					if(menu.data.menu.length==0){
						this.islogin = false
						this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
							type: 'error',
							center: true
						})
						return false
					}
					this.$TOOL.data.set("MENU", menu.data.menu)
					this.$TOOL.data.set("PERMISSIONS", menu.data.permissions)
					this.$TOOL.data.set("DASHBOARDGRID", menu.data.dashboardGrid)
				}else{
					this.islogin = false
					this.$message.warning(menu.message)
					return false
				}
				// 执行页面跳转【替换】
				this.$router.replace({
					path: '/'
				})
				this.$message.success("Login Success 登录成功")
			}
		}
	}
</script>

<style>

</style>
